//-----------------------------------------------------------------------
// <copyright file="a_RelProductsCatDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_RelProductsCatDAOBase
    {
        #region Common methods
        public virtual a_RelProductsCat Createa_RelProductsCatFromReader(IDataReader reader)
        {
            a_RelProductsCat item = new a_RelProductsCat();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("RpCatID"))) item.RpCatID = (int)reader["RpCatID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("StoreID"))) item.StoreID = (int)reader["StoreID"];
                if (!reader.IsDBNull(reader.GetOrdinal("DomainID"))) item.DomainID = (int)reader["DomainID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Industry"))) item.Industry = (string)reader["Industry"];
                if (!reader.IsDBNull(reader.GetOrdinal("TemprpCatID"))) item.TemprpCatID = (int)reader["TemprpCatID"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_RelProductsCat methods
            
        public virtual void Createa_RelProductsCat(a_RelProductsCat a_RelProductsCat)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_RelProductsCat_ins");
                
                database.AddInParameter(dbCommand, "@Name", DbType.AnsiString, a_RelProductsCat.Name);
                database.AddInParameter(dbCommand, "@StoreID", DbType.Int32, a_RelProductsCat.StoreID);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_RelProductsCat.DomainID);
                database.AddInParameter(dbCommand, "@Industry", DbType.AnsiString, a_RelProductsCat.Industry);
                database.AddInParameter(dbCommand, "@TemprpCatID", DbType.Int32, a_RelProductsCat.TemprpCatID);
                database.AddOutParameter(dbCommand, "@RpCatID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_RelProductsCat.RpCatID = (int)database.GetParameterValue(dbCommand, "@RpCatID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_RelProductsCat methods
        
        public virtual void Updatea_RelProductsCat(a_RelProductsCat a_RelProductsCat)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_RelProductsCat_upd");            
                
                database.AddInParameter(dbCommand, "@RpCatID", DbType.Int32, a_RelProductsCat.RpCatID);
                database.AddInParameter(dbCommand, "@Name", DbType.AnsiString, a_RelProductsCat.Name);
                database.AddInParameter(dbCommand, "@StoreID", DbType.Int32, a_RelProductsCat.StoreID);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_RelProductsCat.DomainID);
                database.AddInParameter(dbCommand, "@Industry", DbType.AnsiString, a_RelProductsCat.Industry);
                database.AddInParameter(dbCommand, "@TemprpCatID", DbType.Int32, a_RelProductsCat.TemprpCatID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_RelProductsCat methods
        public virtual void Deletea_RelProductsCat(int rpCatID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_RelProductsCat_del");
                
                database.AddInParameter(dbCommand, "@RpCatID", DbType.Int32, rpCatID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_RelProductsCat methods
        
        public virtual a_RelProductsCat Geta_RelProductsCat(int rpCatID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_RelProductsCat_sel");
                
                database.AddInParameter(dbCommand, "@RpCatID", DbType.Int32, rpCatID);
                
                a_RelProductsCat a_RelProductsCat =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_RelProductsCat = Createa_RelProductsCatFromReader(reader);
                        reader.Close();
                    }
                }
                return a_RelProductsCat;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_RelProductsCatList methods
        public virtual a_RelProductsCatCollection Geta_RelProductsCatList(a_RelProductsCatColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_RelProductsCat_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_RelProductsCatCollection a_RelProductsCatCollection = new a_RelProductsCatCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_RelProductsCat a_RelProductsCat = Createa_RelProductsCatFromReader(reader);
                        a_RelProductsCatCollection.Add(a_RelProductsCat);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_RelProductsCatCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_RelProductsCatCollection Geta_RelProductsCatList(a_RelProductsCatColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_RelProductsCatList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}